/*! ******************************************************************************
 *
 * Created on 2021年9月25日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.util;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.pentaho.di.core.util.StringUtil;
import org.pentaho.di.version.BuildVersion;

import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 通用日志工具类,提供了debug, info, warn, error四个级别日志的通用访问途径。
 *
 * @author tanp@yzbdl.ac.cneng@yzbdl.ac.cn
 */
public class VersionUtil {

    private static final String UNKNOWN = "Unknown";

    static BuildVersion buildVersion = BuildVersion.getInstance();

    /**
     * 获取客户端名称
     *
     * @return String
     */
    public static String getProductName() {
        return VersionUtil.buildVersion.getProductName();
    }

    /**
     * 获取调度平台名称
     *
     * @return String
     */
    public static String getScheduleName() {
        return VersionUtil.buildVersion.getScheduleName();
    }

    /**
     * 获取客户端版本
     *
     * @return String
     */
    public static String getLaniusVersion() {
        return VersionUtil.buildVersion.getVersion();
    }

    /**
     * 获取客户端版本
     *
     * @return String
     */
    public static String getLaniusVersionNoBuildTime() {
        String versionStr = VersionUtil.buildVersion.getVersion();
        if (StringUtils.isNotBlank(versionStr) && !UNKNOWN.equals(versionStr)) {
            return versionStr.substring(0, StringUtil.getCharacterPosition(versionStr, "\\.", 3));
        }
        return "2.x.x";
    }

    /**
     * 获取客户端前两位版本
     *
     * @return String
     */
    public static String getLaniusVersionTopTwo() {
        String versionStr = VersionUtil.buildVersion.getVersion();
        if (StringUtils.isNotBlank(versionStr) && !UNKNOWN.equals(versionStr)) {
            return versionStr.substring(0, StringUtil.getCharacterPosition(versionStr, "\\.", 2));
        }
        try {
            MavenXpp3Reader reader = new MavenXpp3Reader();
            Model model = reader.read(new FileReader("pom.xml"));
            String reg = "^[0-9]+.[0-9]+";
            // 编译正则表达式
            Pattern patten = Pattern.compile(reg);
            // 指定要匹配的字符串
            Matcher matcher = patten.matcher(model.getVersion());
            matcher.find();
            return matcher.group();
        } catch (Exception e) {
            //
        }
        return "1.0";
    }

    /**
     * 获取客户端大版本
     *
     * @return String
     */
    public static String getLaniusTopVersion() {
        StringBuilder backVersion = new StringBuilder();
        String versionStr = VersionUtil.buildVersion.getVersion();
        if (StringUtils.isNotBlank(versionStr) && !UNKNOWN.equals(versionStr)) {
            backVersion.append(versionStr, 0, StringUtil.getCharacterPosition(versionStr, "\\.", 1))
                    .append(".ALL");
            return backVersion.toString();
        }
        try {
            MavenXpp3Reader reader = new MavenXpp3Reader();
            Model model = reader.read(new FileReader("pom.xml"));
            String reg = "^[0-9]+";
            // 编译正则表达式
            Pattern patten = Pattern.compile(reg);
            // 指定要匹配的字符串
            Matcher matcher = patten.matcher(model.getVersion());
            matcher.find();
            backVersion.append(matcher.group()).append(".ALL");
            return backVersion.toString();
        } catch (Exception e) {
            //
        }
        return "1.ALL";
    }
}
